<template>
  <div class="wg-cap-wrap">
    <div class="wg-cap-wrap__header">
      <span>请在下图<em>依次</em>点击：</span>
      <img class="wg-cap-wrap__thumb" v-if="thumbBase64" :src="thumbBase64" alt=" ">
    </div>
    <div class="wg-cap-wrap__body" :style="style">
      <img class="wg-cap-wrap__picture" v-if="imageBase64" :src="imageBase64" alt=" " @click="handleClickPos($event)">
      <img class="wg-cap-wrap__loading" src="" alt="正在加载中...">
      <template v-for="(dot, key) in dots">
        <div :key="key" class="wg-cap-wrap__dot" :style="`top: ${dot.y}px; left:${dot.x}px;`">
          <span>{{ dot.index }}</span>
        </div>
      </template>
    </div>
    <p class="error">
      <span v-text="errorPrompt?errorPrompt+',请刷新重试':''"></span>
    </p>
    <div class="wg-cap-wrap__footer">
      <div class="wg-cap-wrap__ico">
        <img @click="handleCloseEvent"
             src=""
             alt="关闭">
        <img @click="handleRefreshEvent"
             src=""
             alt="刷新">
      </div>
      <div class="wg-cap-wrap__btn">
        <button @click="handleConfirmEvent">确认</button>
      </div>
    </div>
  </div>
</template>

<script>
    export default {
        name: 'GoCaptcha',
        props: {
            value: Boolean,
            width: {
                type: String,
                default: '300px'
            },
            height: {
                type: String,
                default: '240px'
            },
            calcPosType: {
                type: String,
                default: 'dom',
                validator: value => ['dom', 'screen'].includes(value)
            },
            maxDot: {
                type: Number,
                default: 5
                // validator: value => value > 10
            },
            imageBase64: String,
            thumbBase64: String,
            errorPrompt: String  // 错误提示
        },
        data() {
            return {
                dots: [],
                // imageBase64Code: '',
                // thumbBase64Code: ''
            }
        },
        watch: {
            value() {
                this.dots = [];
                // this.imageBase64Code = '';
                // this.thumbBase64Code = ''
            },
            // imageBase64(val) {
            //     this.dots = [];
            //     this.imageBase64Code = val
            // },
            // thumbBase64(val) {
            //     this.dots = [];
            //     this.thumbBase64Code = val
            // }
        },
        computed: {
            style() {
                return `width:${this.width}; height:${this.height};`
            }
        },
        mounted() {
            // console.log(1);
        },
        methods: {
            // 点击关闭
            handleCloseEvent() {
                this.dots = [];
                this.$emit('close')
            },
            // 点击刷新
            handleRefreshEvent() {
                this.dots = [];
                this.$emit('refresh')
            },
            // 点击确认
            handleConfirmEvent() {
                this.$emit('confirm', this.dots)
            },
            // 处理dot(点选定位)
            handleClickPos(ev) {
                // 未处理
                if (this.dots.length >= this.maxDot) {
                    return
                }
                const e = ev || window.event;
                e.preventDefault();
                const dom = e.currentTarget;
                const {domX, domY} = this.getDomXY(dom);

                let mouseX = (navigator.appName === 'Netscape') ? e.pageX : e.x + document.body.offsetTop;
                let mouseY = (navigator.appName === 'Netscape') ? e.pageY : e.y + document.body.offsetTop;

                if (this.calcPosType === 'screen') {
                    mouseX = (navigator.appName === 'Netscape') ? e.clientX : e.x;
                    mouseY = (navigator.appName === 'Netscape') ? e.clientY : e.y;
                }

                // 计算点击的相对位置
                const xPos = mouseX - domX;
                const yPos = mouseY - domY;

                // 转整形
                const xp = parseInt(xPos.toString());
                const yp = parseInt(yPos.toString());

                // 减去点的一半
                this.dots.push({
                    x: xp - 11,
                    y: yp - 11,
                    index: this.dots.length + 1
                });
                return false
            },
            // 找到元素的屏幕位置
            getDomXY(dom){
                let x = 0;
                let y = 0;
                if (dom.getBoundingClientRect) {
                    let box = dom.getBoundingClientRect();
                    let D = document.documentElement;
                    x = box.left + Math.max(D.scrollLeft, document.body.scrollLeft) - D.clientLeft;
                    y = box.top + Math.max(D.scrollTop, document.body.scrollTop) - D.clientTop
                } else{
                    while (dom !== document.body) {
                        x += dom.offsetLeft;
                        y += dom.offsetTop;
                        dom = dom.offsetParent
                    }
                }
                return {
                    domX: x,
                    domY: y
                }
            }
        }
    }
</script>

<style>
  p.error{
    height: 10px;
  }
  p.error span{
    color: #f00;
    font-size: 0.6rem;
  }
  .wg-cap-wrap{
    width: 100%;
    height: 100%;
    background: #ffffff;

    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;

    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }
  .wg-cap-wrap__header{
    height: 50px;
    width: 100%;
    font-size: 15px;

    display:-webkit-box;
    display:-webkit-flex;
    display:-ms-flexbox;
    display:flex;
    -webkit-box-align:center;
    -webkit-align-items:center;
    -ms-flex-align:center;
    align-items: center;
  }
  .wg-cap-wrap__header span{
    padding-right: 5px;
  }
  .wg-cap-wrap__header span em{
    padding: 0 3px;
    font-weight: bold;
    color: #3e7cff;
    font-style: normal;
  }
  .wg-cap-wrap__header .wg-cap-wrap__image{
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    overflow: hidden;
    text-align: center;
    line-height: 1;
  }
  .wg-cap-wrap__header .wg-cap-wrap__thumb{
    min-width: 150px;
    text-align: center;
    line-height: 1;
    max-height: 100%;
  }
  .wg-cap-wrap__header .wg-cap-wrap__thumb.wg-cap-wrap__hidden{
    display: none;
  }

  .wg-cap-wrap__body{
    position: relative;
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    background: #34383e;
    margin: auto;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    overflow: hidden;
  }
  .wg-cap-wrap__body .wg-cap-wrap__picture{
    position: relative;
    z-index: 10;
    width: 100%;

    /*height: 100%;*/
    /*max-width: 100%;*/
    /*max-height: 100%;*/
    /*object-fit: cover;*/
    /*text-align: center;*/
  }
  .wg-cap-wrap__body .wg-cap-wrap__picture.wg-cap-wrap__hidden{
    display: none;
  }
  .wg-cap-wrap__body .wg-cap-wrap__loading{
    position: absolute;
    z-index: 9;
    top: 50%;
    left: 50%;
    width: 68px;
    height: 68px;
    margin-left: -34px;
    margin-top: -34px;
    line-height: 68px;
    text-align: center;
  }
  .wg-cap-wrap__body .wg-cap-wrap__dot{
    position: absolute;
    z-index: 10;
    width: 22px;
    height: 22px;
    color: #cedffe;
    background: #3e7cff;
    border: 2px solid #f7f9fb;
    line-height: 20px;
    text-align: center;
    -webkit-border-radius: 22px;
    -moz-border-radius: 22px;
    border-radius: 22px;
    cursor: default;
  }

  .wg-cap-wrap__footer {
    width: 100%;
    height: 40px;
    color: #34383e;
    display:-webkit-box;
    display:-webkit-flex;
    display:-ms-flexbox;
    display:flex;
    -webkit-box-align:center;
    -webkit-align-items:center;
    -ms-flex-align:center;
    align-items: center;
    padding-top: 15px;
  }
  .wg-cap-wrap__footer .wg-cap-wrap__ico{
    flex: 1;
  }
  .wg-cap-wrap__footer .wg-cap-wrap__ico img{
    width: 24px;
    height: 24px;
    color: #34383e;
    margin: 0 5px;
    cursor: pointer;
  }
  .wg-cap-wrap__footer .wg-cap-wrap__btn{
    width: 120px;
    height: 40px;
  }
  .wg-cap-wrap__footer .wg-cap-wrap__btn button{
    width: 100%;
    height: 40px;
    letter-spacing: 2px;
    text-align: center;
    padding: 9px 15px;
    font-size: 15px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    display: inline-block;
    line-height: 1;
    white-space: nowrap;
    cursor: pointer;
    color: #fff;
    background-color: #409eff;
    border: 1px solid #409eff;
    -webkit-appearance: none;
    box-sizing: border-box;
    outline: none;
    margin: 0;
    transition: .1s;
    font-weight: 500;
    -moz-user-select: none;
    -webkit-user-select: none;
  }
  .wg-cap-wrap__footer .wg-cap-wrap__btn button:hover {
    background: #66b1ff;
    border-color: #66b1ff;
    color: #fff;
  }
</style>